package cn.jiayen.example.service.impl;

import cn.jiayen.example.bean.User;
import cn.jiayen.mvc.error.CustomParameterizedException;
import cn.jiayen.mvc.page.PagerVO;
import cn.jiayen.example.repository.UserRepository;
import cn.jiayen.example.service.UserService;
import cn.jiayen.example.views.dto.UserDTO;
import cn.jiayen.example.views.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import cn.jiayen.comm.util.CustomerStringUtils;
/**
 * Created by jiayen on 2018/5/5.
 */
@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;

    @Override
    public UserVO add(UserDTO user) {
        User bean = new User();
        BeanUtils.copyProperties(user,bean);
        userRepository.save(bean);
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(bean,userVO);
        return userVO;
    }

    @Transactional(rollbackOn = Exception.class)
    @Override
    public void deleteById(String ids) {
        if(StringUtils.isEmpty(ids)){
        throw  new CustomParameterizedException("请输入要操作的id");
        }
        try {
             userRepository.deleteByIds(CustomerStringUtils.strToList(ids));
        }catch (Exception e){
             log.error("删除失败:",e);
            throw  new CustomParameterizedException("删除失败，请稍后再试!");
        }
    }

    @Override
    public UserVO update(UserDTO user) {
        Integer id = user.getId();
        if(StringUtils.isEmpty(id)){
            throw  new CustomParameterizedException("请输入正确更新的数据");
        }
        User bean = userRepository.getOne(id);
        BeanUtils.copyProperties(user,bean);
        userRepository.save(bean);
        UserVO vo = new UserVO();
        BeanUtils.copyProperties(bean,vo);
        return vo;
    }

    @Override
    public UserVO getById(Integer id) {
        if(StringUtils.isEmpty(id)){
            throw  new CustomParameterizedException("请输入正确的id");
        }
        try {
            User bean = userRepository.getOne(id);
            UserVO vo = new UserVO();
            BeanUtils.copyProperties(bean,vo);
            return vo;
        }catch (Exception e){
            log.error("该条数据不存在:",e);
            throw  new CustomParameterizedException("该条数据不存在!");
        }

    }

    @Override
    public PagerVO<UserVO> getUserList(Integer page, Integer pageSize) {
        //默认按照id倒序排序
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        if (page < 1) {
            page = 1;
        }
        Pageable pageable =  PageRequest.of(page-1, pageSize, sort);
        Page<User> pageContent = userRepository.findAll(pageable);

        List<User> list = pageContent.getContent();
        List<UserVO> userVOList = new ArrayList<>();
        list.stream().forEach(user -> {
            UserVO vo = new UserVO();
            BeanUtils.copyProperties(user,vo);
            userVOList.add(vo);
        });
        PagerVO<UserVO> pagerVO =new PagerVO<>(page,pageSize,pageContent.getTotalElements());
        pagerVO.setRows(userVOList);
        return pagerVO;
    }
}
